package com.ybkj.chat.module.mine.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.ybkj.chat.R;
import com.ybkj.chat.base.BaseMvpActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author yyj
 * @create 2019-02-22 16:00
 * @Describe 车辆认证
 */
public class CarActivity extends BaseMvpActivity {
    @BindView(R.id.tv_main)
    TextView tvMain;
    @BindView(R.id.tv_second)
    TextView tvSecond;
    @BindView(R.id.iv_car_main)
    ImageView ivCarMain;
    @BindView(R.id.iv_car_second)
    ImageView ivCarSecond;
    @BindView(R.id.iv_scan_main)
    ImageView ivScanMain;
    @BindView(R.id.iv_scan_second)
    ImageView ivScanSecond;

    private static final int CAMERA_MAIN_CHOOSE = 110;
    private static final int CAMERA_SECOND_CHOOSE = 111;

    @Override
    protected void injectPresenter() {

    }

    @Override
    protected void initTitle() {
        setTitleText("车辆认证");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_car;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        tvMain.setText(Html.fromHtml("点击开始识别行驶证 <font color=\"#638DFD\">主页</font>"));
        tvSecond.setText(Html.fromHtml("点击开始识别行驶证 <font color=\"#638DFD\">副页</font>"));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.rtl_take_photo1, R.id.rtl_take_photo2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rtl_take_photo1:
                PictureSelector.create(CarActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .enableCrop(true)
                        .rotateEnabled(false)
                        .forResult(CAMERA_MAIN_CHOOSE);
                break;
            case R.id.rtl_take_photo2:
                PictureSelector.create(CarActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .enableCrop(true)
                        .rotateEnabled(false)
                        .forResult(CAMERA_SECOND_CHOOSE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            // 图片、视频、音频选择结果回调
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            switch (requestCode) {
                case CAMERA_MAIN_CHOOSE:
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    loadImage(selectList.get(0), ivCarMain,ivScanMain);
                    break;
                case CAMERA_SECOND_CHOOSE:
                    loadImage(selectList.get(0), ivCarSecond,ivScanSecond);
                    break;
            }
        }
    }

    //拍完照后加载图片
    private void loadImage(LocalMedia media, ImageView imageView,ImageView imageView2) {
        String path = "";
        if (media.isCut() && !media.isCompressed()) {
            // 裁剪过
            path = media.getCutPath();
        } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
            // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
            path = media.getCompressPath();
        } else {
            // 原图
            path = media.getPath();
        }
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.color.color_f6)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(this)
                .load(path)
                .apply(options)
                .into(imageView);
        imageView2.setVisibility(View.GONE);

    }
}
